LaTeX Lecture¶
Sage can be useful for formatting mathematics in $\LaTeX$, which can then be pasted into markdown cells.
You can also use it to learn $\LaTeX$. Some other references for learning $\LaTeX$:
- Adding Math to $\LaTeX$: Part of a tutorial from Overleaf.
- Detexify: A tool to find LaTeX symbols by drawing them.
- LaTeX: A Document Preparation System by Leslie Lamport: An in-depth guide to LaTeX (book).
- LaTeX Project: The official LaTeX Project website, offering comprehensive documentation.
For using LaTeX with Sage, see the tutorial Sage, LaTeX and Friends.
Using LaTeX within Markdown cells¶
You can enclose math in single dollar signs to include math inline. For example $3+5-13$
produces $3+5-13$.
If you use double dollar signs, you get a centered equation. For example $$(5, 3/4)$$
produces
$$(5, 3/4)$$
Exporting LaTeX from Sage expressions¶
You can use the latex()
command to convert a Sage object or expression into $\LaTeX$. Below we do this to a Sage expression:
var('x')
expression = sin(x)
expression
sin(x)
latex(expression)
\sin\left(x\right)
var('x')
expression = sin(x^3/(x-1))
expression
sin(x^3/(x - 1))
latex(expression)
\sin\left(\frac{x^{3}}{x - 1}\right)
You can cut and paste the result in between dollar signs in a Markdown cell: $\sin\left(x\right)$
gives $\sin\left(x\right)$.
Maybe we want to typeset the following equation:
eq = sin(pi/2) == 1
eq
1 == 1
Notice that Sage automatically evaluates sin(pi/2)
. To fix this, you can use the hold
option:
eq = sin(pi/2, hold=True) == 1
eq
sin(1/2*pi) == 1
latex(eq)
\sin\left(\frac{1}{2} \, \pi\right) = 1
Pasting the above in between dollar signs yields $\sin\left(\frac{1}{2} \, \pi\right) = 1$.
Remark: If you know some $\LaTeX$, it is often useful to improve things yourself based on the Sage output. For example, I would prefer to write this equation as
$\sin\left(\frac{\pi}{2}\right) = 1$
which yields $\sin\left(\frac{\pi}{2}\right) = 1$.
Greek variables¶
If you introduce variables named after Greek letters then they display correctly in Sage.
# Define the probability density function for the normal distribution function symbolically
var('x mu sigma')
F(x) = 1 / (sigma * sqrt(2 * pi)) * exp(-1/2 * ((x - mu)/sigma)^2)
show(F)
latex(F)
x \ {\mapsto}\ \frac{\sqrt{2} e^{\left(-\frac{{\left(\mu - x\right)}^{2}}{2 \, \sigma^{2}}\right)}}{2 \, \sqrt{\pi} \sigma}
Inserting it between double dollar signs yields. $$x \ {\mapsto}\ \frac{\sqrt{2} e^{\left(-\frac{{\left(\mu - x\right)}^{2}}{2 \, \sigma^{2}}\right)}}{2 \, \sqrt{\pi} \sigma}$$
The word lambda
is a keyword in Python, so we use lambda_
instead.
show(var('lambda_'))
Capital greek letters are okay as well:
show(var('Gamma'))
Fine tuning latex variables¶
You can also declare exactly how a variable is converted to latex using the latex_name
option for var
.
In $\LaTeX$, the command \mathbf
renders the next character in bold. So, we can do:
var('v', latex_name = r'{\mathbf v}')
show(v)
var('v', latex_name = r'{\overrightarrow v}')
show(v)
var('v', latex_name = r'{\overrightarrow v}')
show(v)
print("Hi \n There")
Hi There
We use r'...'
to get a raw string, a string that will not be editted by Sage. This is because backslashes are used in Sage and Python for special characters. For example, \n
denotes a newline character. We get errors above if we don't use a raw string because of the presence of the backslash. (In Python/Sage, \m
has no meaning and leads to an error.)
Because Python converts \\
to \
within a string, we can also write the above as:
var('v', latex_name = '{\\mathbf v}')
show(v)
This is useful if for example you want to make use of some of the features of Python's rewriting rules for strings (e.g., to make use of f-string formatting).
Once the variable v
is defined by either method, we can write the eigenvector equation:
var('M')
var('lambda_')
eq = M*v == lambda_*v
eq
M*v == lambda_*v
latex(eq)
M {{\mathbf v}} = \lambda {{\mathbf v}}
Pasting in between dollar signs yields: $$M {{\overrightarrow v}} = \lambda {{\overrightarrow v}}$$
Super and subscripts¶
A cool thing about Sage is that you can learn LaTeX by using Sage. For example, if you want to learn how to write superscripts in LaTeX, you can write an expression in Sage:
var('x n')
expression = x^n
latex(expression)
x^{n}
Here is what we get when we paste between dollar signs: $x^{n}$.
You can see from the above that you can get a superscript or exponent using the caret ^
. The caret should be followed by an expression enclosed with curly brackets {... }
, what is inside the brackets are placed in the exponent. So, if you remember that \sin
is used in LaTeX for the sine function and have learned that for greek letters you can write things like \theta
, we can write
x^{\sin \theta}
to get
$$x^{\sin \theta}$$
Summation¶
var('n')
divergent = sum(1/n, n, 1, infinity, hold=True) == infinity
divergent
sum(1/n, n, 1, +Infinity) == +Infinity
show(divergent)
latex(divergent)
{\sum_{n=1}^{+\infty} \frac{1}{n}} = +\infty
Pasting into latex yields: $${\sum_{n=1}^{+\infty} \frac{1}{n}} = +\infty$$
Subscripts¶
In the sum an underscore _
is used to denote what is under the summation. Underscores are also used for subscripts. For example we can write \Gamma_{i,j}
for $\Gamma_{i,j}$.
Matrices¶
Let's learn how to format a matrix in $\LaTeX$. We already learned how to create matrices in Sage. For example:
x = var('x')
M = matrix([
[ 1, -2, 3 ],
[ -4, x, -6 ],
[ 7, -8, 9 ]
])
M
[ 1 -2 3] [-4 x -6] [ 7 -8 9]
latex(M)
\left(\begin{array}{rrr} 1 & -2 & 3 \\ -4 & x & -6 \\ 7 & -8 & 9 \end{array}\right)
Pasting in between dollar signs yields: $$\left(\begin{array}{rrr} 1 & -2 & 3 \\ -4 & x & -6 \\ 7 & -8 & 9 \end{array}\right)$$
Some information about this:
- The
\left(
and\right)
are the outer parenthesis. \begin{array}
indicates the start of the matrix and\end{array}
the end.- The
{rrr}
indicates that there are three columns and all should be right-aligned. - Entries in a row are separated by
&
and we move to the next row using\\
.
You can change the brackets to square brackets:
\left[\begin{array}{rrr}
1 & -2 & 3 \\
-4 & x & -6 \\
7 & -8 & 9
\end{array}\right]
yields $$\left[\begin{array}{rrr} 1 & -2 & 3 \\ -4 & x & -6 \\ 7 & -8 & 9 \end{array}\right]$$
Or you can use curly brackets. To write curly brackets you use \{
and \}
because {...}
groups an expression in LaTeX. So
\left\{\begin{array}{rrr}
1 & -2 & 3 \\
-4 & x & -6 \\
7 & -8 & 9
\end{array}\right\}
yields $$\left\{\begin{array}{rrr} 1 & -2 & 3 \\ -4 & x & -6 \\ 7 & -8 & 9 \end{array}\right\}$$
When I write LaTeX by hand, I prefer to use \begin{pmatrix}...\end{pmatrix}
which takes care of the parenthesis and doesn't require the column alignment options. So, I'd write the above with:
\begin{pmatrix}
1 & -2 & 3 \\
-4 & x & -6 \\
7 & -8 & 9
\end{pmatrix}
which yields $$\begin{pmatrix} 1 & -2 & 3 \\ -4 & x & -6 \\ 7 & -8 & 9 \end{pmatrix}$$
You can read more about pmatrix
and related LaTeX commands on overleaf's tutorial.
Problem 1:¶
Write a function variable_matrix(letter, n, m)
that returns an $n \times m$ matrix (with $n$ rows and $m$ columns) such that the entry in row $i$ and column $j$ is a Sage variable with name x_i_j
with x
being the letter. When the matrix is converted to $\LaTeX$, the entries should display as x_{i, j}
which becomes $x_{i,j}$ when typeset.
Sets:¶
Latex has sets like the integers built in. By default they display as bold symbols:
print(latex(ZZ))
show(ZZ)
\Bold{Z}
If you prefer blackboard bold, run the command:
latex.blackboard_bold(True)
print(latex(ZZ))
show(ZZ)
\Bold{Z}
What is happening is that Sage has a built in macro \Bold
that is being changed. So, the latex code doesn't change but the rendering of \Bold
does.
Anyway, if you want to typeset a bold symbol use for example \mathbf X
which yields $\mathbf X$. To typeset it as blackboard bold use \mathbb X
which yields $\mathbb X$. Within Sage you can use \Bold
which will depend on the value of latex.blackboard_bold()
. But you'll need to change it from \Bold
if you want to paste into a regular $\LaTeX$ document, or include a macro definition in your document.